package com.yanqu.road.server.logic.thread;

import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class UnionReportTask extends GeneralTask {
    private Logger logger = LogManager.getLogger(UnionReportTask.class.getName());

    private String guildID;
    private String serverID;
    private String type;
    private String content;

    public UnionReportTask(String guildID, String serverID, String type, String content) {
        super();
        this.guildID = guildID;
        this.serverID = serverID;
        this.type = type;
        this.content = content;
    }

    @Override
    public void doRun() {
        SortedMap<String, String> paramMap = new TreeMap<>();
        try {
            paramMap.put("guildID", guildID);
            paramMap.put("serverID", serverID);
            paramMap.put("type", type);
            paramMap.put("content", URLEncoder.encode(content, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String sign = SignHelper.getSign(paramMap, AppKeyMgr.getAndroidAppSecret());
        paramMap.put("sign", sign);
        try {
            String url = AppKeyMgr.getPlatformBaseUrl() + "/chat/unionReporting/" + AppKeyMgr.getAndroidAppId() + "/" + AppKeyMgr.getPlatformChannelId();
            logger.info("UnionReport url:{}", url);
            Map<String, String> resultMap = HttpHelper.doGet(url, paramMap);
            if(null == resultMap){
                logger.info("UnionReport request error");
            }else {
                logger.info("UnionReport code:{}; result:{}", resultMap.get("code"), resultMap.get("body"));
            }
        } catch (Exception e) {
            logger.error("UnionReport error:",e);
        }
    }
}
